﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Data.SqlClient;

namespace SqlExecuteHelper
{
    public static class ConfigHelper
    {
        private static string ConfigPath
        {
            get { return Path.Combine(AppDomain.CurrentDomain.BaseDirectory,"Config"); }
        }

        private static string ConfigFileName
        {
            get { return "server.xml"; }
        }

        public static void AddConfig(string serverName, string serverIp, string password)
        {
            if (!Directory.Exists(ConfigPath))
            {
                Directory.CreateDirectory(ConfigPath);
            }
            var fullPath = Path.Combine(ConfigPath, ConfigFileName);
            var xmlDoc = new XmlDocument();
            XmlNode rootNode;
            if (!File.Exists(fullPath))
            {
                var xmldecl = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);
                xmlDoc.AppendChild(xmldecl);
                rootNode = xmlDoc.CreateElement("Servers");
                xmlDoc.AppendChild(rootNode);
            }
            else
            {
                xmlDoc.Load(fullPath);
                rootNode = xmlDoc.SelectSingleNode("Servers");
            }

            var configItem = xmlDoc.CreateElement("item");
            configItem.SetAttribute("name", serverName);
            configItem.SetAttribute("data-source", serverIp);
            configItem.SetAttribute("password", password);
            rootNode.AppendChild(configItem);
            xmlDoc.Save(fullPath);
        }

        public static void RemoveConfig(string serverName)
        {
            var fullPath = Path.Combine(ConfigPath, ConfigFileName);
            var xmlDoc = new XmlDocument();
            XmlNode rootNode;
            if (File.Exists(fullPath))
            {
                xmlDoc.Load(fullPath);
                rootNode = xmlDoc.SelectSingleNode("Servers");
                var removeItem = rootNode.SelectSingleNode(string.Format("//item[@name='{0}']", serverName));
                removeItem.ParentNode.RemoveChild(removeItem);
                xmlDoc.Save(fullPath);
            }
        }

        public static List<string> GetServerNames()
        {
            var configFullPath = Path.Combine(ConfigPath, ConfigFileName);
            var serverNames = new List<string>();
            if (File.Exists(configFullPath))
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(configFullPath);
                var roodNode = xmlDoc.SelectSingleNode("Servers");
                var nodes = roodNode.ChildNodes;
                foreach (XmlNode node in nodes)
                {
                    serverNames.Add(node.Attributes["name"].Value);
                }
            }
            return serverNames;
        }

        public static SqlConnectionStringBuilder GetServerConnectionString(string serverName)
        {
            SqlConnectionStringBuilder connectionStringBuilder = null;
            var configFullPath = Path.Combine(ConfigPath, ConfigFileName);
            if (File.Exists(configFullPath) && !string.IsNullOrEmpty(serverName))
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(configFullPath);
                var rootNode = xmlDoc.SelectSingleNode("Servers");
                var selectItem = rootNode.SelectSingleNode(string.Format("item[@name='{0}']", serverName));

                //连接字符串builder
                connectionStringBuilder = new SqlConnectionStringBuilder();
                connectionStringBuilder.DataSource = selectItem.Attributes["data-source"].Value;
                connectionStringBuilder.InitialCatalog = "AdminCenter";
                connectionStringBuilder.PersistSecurityInfo = true;
                connectionStringBuilder.UserID = "sa";
                connectionStringBuilder.Password = selectItem.Attributes["password"].Value;

                //8.83的是特殊
                if (selectItem.Attributes["data-source"].Value.StartsWith( "192.168.8.184"))
                {
                    connectionStringBuilder.InitialCatalog = "AdminCenterDB";
                }
            }
            return connectionStringBuilder;
        }

        public static void AlterConfig(string serverName, string serverIp, string password)
        {
            var fullPath = Path.Combine(ConfigPath, ConfigFileName);
            var xmlDoc = new XmlDocument();
            if (File.Exists(fullPath))
            {
                xmlDoc.Load(fullPath);
                var rootNode = xmlDoc.SelectSingleNode("Servers");
                var alterItem = rootNode.SelectSingleNode(string.Format("item[@name='{0}']", serverName));
                if (alterItem != null)
                {
                    alterItem.Attributes["data-source"].Value = serverIp;
                    alterItem.Attributes["password"].Value = password;
                }
                xmlDoc.Save(fullPath);
            }
        }
    }
}
